//
//  WelcomeController.swift
//  MySinaWeiBo
//
//  Created by zhangfuwei on 15/7/30.
//  Copyright © 2015年 zhangfuwei. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeController: UIViewController {

    //  iconView的 Y 方向的约束
    
    var iconViewBottomContraint : NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        preparUI()
        if let urlString = UserAccount.loadUserAcount()?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 提示：修改约束不会立即生效，添加了一个标记，统一由自动布局系统更新约束
        iconViewBottomContraint?.constant = -UIScreen.mainScreen().bounds.height - iconViewBottomContraint!.constant
        
        // 动画
        
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //  更新 UI
            self.view.layoutIfNeeded()
            
            }) { (_ ) -> Void in
                
                print("切换到主控制器")
                NSNotificationCenter.defaultCenter().postNotificationName(FWNotification, object: true)
        }
    
    }
    
    ///  更新 UI
    func preparUI() {
        view.addSubview(backgroudView)
        view.addSubview(iconView)
        view.addSubview(messageLbl)
        
        /// 设置 backgroundView 的约束
        backgroudView.ff_Fill(view)

        ///  设置 iconView 的约束
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size:CGSize(width: 90, height: 90) , offset: CGPoint(x: 0, y: -160))
        
        iconViewBottomContraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        /// 设置 messageLbl

        messageLbl.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 14))
        
    }
    
    //  懒加载 backgroudView , iconView , messageLbl
    lazy var backgroudView : UIImageView = {
       
        let v = UIImageView()
        v.image = UIImage(named: "ad_background")
        return v
        
    }()
    
    lazy var iconView : UIImageView = {
        let v = UIImageView()
        
        v.image = UIImage(named: "avatar_default_big")
        v.layer.cornerRadius = 45
        v.layer.masksToBounds = true
        return v
    }()
    
    lazy var messageLbl : UILabel = {
        let lbl = UILabel()
        lbl.text = "欢迎归来"
    
        lbl.sizeToFit()
        return lbl
    }()

}
